package jgl.vajra.operator.part.home.fragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.OrderListBean;
import jgl.vajra.operator.event.OrderPageListEvent;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.home.contract.OrderPageListContract;
import jgl.vajra.operator.part.home.viewModel.OrderPageListViewModel;
import jgl.vajra.operator.widget.base.BaseListFragment;
import jgl.vajra.operator.widget.bean.BaseListData;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(OrderPageListViewModel.class)
public class OrderPageListFragment extends BaseListFragment<OrderPageListViewModel, OrderListBean> implements BaseBindingItemPresenter<OrderListBean>, OrderPageListContract.View {

    private String authorization;
    private int type;

    @Override
    protected RefreshRecyclerNetConfig getRefreshRecyclerNetConfig() {
        return new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", authorization);
                map.put("type", type);
                return mViewModel.getOrderList(map);
            }
        };
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_order_page_list;
    }

    @Override
    public void initView() {
        super.initView();

        type = getArguments().getInt("type");
        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;

         mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setIsShowPageManager(true);
       /* mBinding.recyclerView.setEmptyRes(R.drawable.ic_order_empty_star);
        mBinding.recyclerView.setEmptyMsg("暂无评论");*/
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<OrderListBean>>() {
            @Override
            public void onSucceed(BaseListData<OrderListBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                for (int i = 0; i < data.data.size(); i++) {
                    data.data.get(i).sizeAddressList = data.data.get(i).address.size();
                    String channelText = "";
                    String addressEnd = "";
                    if (data.data.get(i).address.size() > 2) {
                        int channelNum = data.data.get(i).address.size() - 2;
                        channelText = "经过" + channelNum + "个途径点";
                    }
                    if (data.data.get(i).address.size() >= 2) {
                        List<String> addressList = data.data.get(i).address;
                        addressEnd = addressList.get(addressList.size() - 1);
                    }
                    data.data.get(i).channelText = channelText;
                    data.data.get(i).addressEnd = addressEnd;
                }

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
    }

    @Override
    public void onItemClick(int position, OrderListBean itemData) {
    }

    //详情
    @Override
    public void onDetails(int position, OrderListBean itemData) {
        if (itemData.state==1)
            IntentManager.toOrderDetailsActivity(mActivity, itemData.orderId);
        else {
            IntentManager.toOrderDetailsCompleteActivity(mActivity, itemData.orderId);
        }
    }

    //更换机佬
    @Override
    public void onReplaceDriver(int position, OrderListBean itemData) {
        IntentManager.toReplaceDriverActivity(mActivity,itemData);
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOrderPageListEvent(OrderPageListEvent event) {
        mBinding.recyclerView.firstLoad();
    }

    @Override
    public void onResume() {
        super.onResume();
        mBinding.recyclerView.firstLoad();
    }
}
